package cn.iocoder.yudao.module.member.controller.app.order;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.member.controller.app.order.vo.*;
import cn.iocoder.yudao.module.member.controller.app.user.vo.AppMemberUserVo;
import cn.iocoder.yudao.module.member.dal.dataobject.order.MemberCertificateOrderDo;
import cn.iocoder.yudao.module.member.dal.dataobject.order.MemberOrderDo;
import cn.iocoder.yudao.module.member.service.order.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 App - 订单管理")
@RestController
@RequestMapping("/member/order")
@Validated
public class OrderController {

    @Resource
    private OrderService orderService;

    @PostMapping("/createOrder")
    @Operation(summary = "创建订单")
    @PreAuthenticated
    public CommonResult<Long> createOrder(@Valid @RequestBody AppOrderVo appOrderVo) {
        Long order = orderService.createOrder(appOrderVo);
        return success( order);
    }

    @PostMapping("/update-paid")
    @Operation(summary = "更新钱包充值为已充值") // 由 pay-module 支付服务，进行回调，可见 PayNotifyJob
    @PermitAll // 无需登录， 内部校验实现
    public CommonResult<Boolean> updateWalletRechargerPaid(@Valid @RequestBody PayOrderNotifyReqDTO notifyReqDTO) {
        orderService.updateWalletRechargerPaid(Long.valueOf(notifyReqDTO.getMerchantOrderId()),
                notifyReqDTO.getPayOrderId());
        return success(true);
    }

    //订单分页
    @PostMapping("/page")
    @Operation(summary = "获得充值记录分页")
    @PreAuthenticated
    public CommonResult<PageResult<AppOrderVo>> getRecordPage(
            @Valid @RequestBody AppOrderPageVo pageReqVO) {
        PageResult<MemberOrderDo> pageResult = orderService.getRecordPage(getLoginUserId(), pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppOrderVo.class));
    }



}
